package com.wyd.jwpkglxt_ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyd.jwpkglxt_ssm.dao.ProfessionalTitleDao;
import com.wyd.jwpkglxt_ssm.dao.UserPtDao;
import com.wyd.jwpkglxt_ssm.dao.UserRoleDao;
import com.wyd.jwpkglxt_ssm.entity.ProfessionalTitle;
import com.wyd.jwpkglxt_ssm.entity.User;
import com.wyd.jwpkglxt_ssm.dao.UserDao;
import com.wyd.jwpkglxt_ssm.entity.UserPt;
import com.wyd.jwpkglxt_ssm.entity.UserRole;
import com.wyd.jwpkglxt_ssm.service.UserService;
import com.wyd.jwpkglxt_ssm.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2021-07-22 16:09:45
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private ProfessionalTitleDao professionalTitleDao;

    @Autowired
    private UserPtDao userPtDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        String salt = MD5Util.getSalt();
        int rs = this.userDao.insert(user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword() + salt))
                .setCreateTime(new Timestamp(System.currentTimeMillis())).setSalt(salt));
        if(rs!=0){
            int roleId = user.getRole().getId();
            int rsUserRole = this.userRoleDao.insert(new UserRole().setUserId(user.getId()).setRoleId(roleId)
                    .setCreateTime(new Timestamp(System.currentTimeMillis())));
            if(rsUserRole!=0){
                if(roleId!=1){
                    //讲师身份
                    if(roleId==2){
                        ProfessionalTitle professionalTitle = new ProfessionalTitle();
                        int rsProfessionalTitle = this.professionalTitleDao.insert(professionalTitle.setTitle("讲师")
                                .setRealName(user.getRealName()).setCreateTime(new Timestamp(System.currentTimeMillis())));
                        if(rsProfessionalTitle!=0){
                            int rsUserPt = this.userPtDao.insert(new UserPt().setUserId(user.getId()).setPtId(professionalTitle.getId())
                                    .setCreateTime(new Timestamp(System.currentTimeMillis())));
                            if(rsUserPt!=0){
                                return user;
                            }
                        }
                        //教务总监身份
                    }else if(roleId==3){
                        ProfessionalTitle professionalTitle = new ProfessionalTitle();
                        int rsProfessionalTitle = this.professionalTitleDao.insert(professionalTitle.setTitle("教务总监")
                                .setRealName(user.getRealName()).setCreateTime(new Timestamp(System.currentTimeMillis())));
                        if(rsProfessionalTitle!=0){
                            int rsUserPt = this.userPtDao.insert(new UserPt().setUserId(user.getId()).setPtId(professionalTitle.getId())
                                    .setCreateTime(new Timestamp(System.currentTimeMillis())));
                            if(rsUserPt!=0){
                                return user;
                            }
                        }
                    }

                }
                return user;
            }
        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        if(null!=user.getPassword()&&user.getPassword().length()>0){
            String salt = MD5Util.getSalt();
            int rs = this.userDao.update(user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword() +
                    salt)).setSalt(salt).setUpdateTime(new Timestamp(System.currentTimeMillis())));
            if(rs!=0){
                return this.queryById(user.getId());
            }
        }else{
            //更新用户
            int rsUser = this.userDao.update(user.setUpdateTime(new Timestamp(System.currentTimeMillis())));
            if(rsUser!=0){
                Integer roleId = user.getRole().getId();
                Integer queryUserRoleId = this.userDao.queryByIdWithRole(user.getId()).getRole().getId();
                //不做权限更新
                if(roleId.equals(queryUserRoleId)) {
                    Integer professionalTitleId = this.userPtDao.queryAll(new UserPt().setUserId(user.getId())).get(0).getPtId();
                    if (roleId != 1) {
                        //讲师身份
                        int rsProfessionalTitle = this.professionalTitleDao.update(new ProfessionalTitle().setId(professionalTitleId)
                                .setUpdateTime(new Timestamp(System.currentTimeMillis())).setRealName(user.getRealName()));
                        if (rsProfessionalTitle != 0) {
                            return user;
                        }
                    }
                }
                //更新权限，职称
                if(roleId!=null){
                    Integer userRoleId = this.userRoleDao.queryAll(new UserRole().setUserId(user.getId())).get(0).getId();
                    int rsUserRole = this.userRoleDao.update(new UserRole().setId(userRoleId).setRoleId(roleId)
                            .setUpdateTime(new Timestamp(System.currentTimeMillis())));
                    if(rsUserRole!=0){
                        Integer professionalTitleId = this.userPtDao.queryAll(new UserPt().setUserId(user.getId())).get(0).getPtId();
                        if(roleId!=1){
                            //讲师身份
                            if(roleId==2){
                                int rsProfessionalTitle = this.professionalTitleDao.update(new ProfessionalTitle().setId(professionalTitleId).setTitle("讲师")
                                        .setUpdateTime(new Timestamp(System.currentTimeMillis())).setRealName(user.getRealName()));
                                if(rsProfessionalTitle!=0){
                                    return user;
                                }
                                //教务总监身份
                            }else if(roleId==3){
                                int rsProfessionalTitle = this.professionalTitleDao.update(new ProfessionalTitle().setId(professionalTitleId).setTitle("教务总监")
                                        .setUpdateTime(new Timestamp(System.currentTimeMillis())).setRealName(user.getRealName()));
                                if(rsProfessionalTitle!=0){
                                    return user;
                                }
                            }

                        }

                    }
                }
            }
        }
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }


    @Override
    public User login(String username,String password) {
        User user = new User().setUsername(username);
        List<User> users = this.userDao.queryAllWithRole(user);
        if(!users.isEmpty()) {
            String salt = users.get(0).getSalt();
            List<User> users1 = this.userDao.queryAllWithRole(user.setPassword(MD5Util.MD5EncodeUtf8(password + salt)));
            if (!users1.isEmpty()) {
                return users1.get(0);
            }
        }
        return null;
    }

    @Override
    public User selectByIdWithRole(Integer id) {
        return this.userDao.queryByIdWithRole(id);
    }

    @Override
    public PageInfo<User> selectAllWithRoleByPage(Integer pageNum,Integer pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = this.userDao.queryAllWithRoleAndLike(user);
        return new PageInfo<>(users);
    }

    @Override
    public int updateStatusBatch(List<Integer> userIds,Integer status) {
        return this.userDao.updateStatusBatch(userIds,status,new Timestamp(System.currentTimeMillis()));
    }

    @Override
    public Boolean deleteUserBatch(List<Integer> userIds) {
        return this.userDao.deleteUserBatch(userIds) > 0;
    }


}